﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Text.RegularExpressions;

namespace SiloManaging
{
    public partial class frmIngredient : Form
    {
        string sql = null;
        bool ShowNotUse = false;
        string tmpCode = "", tmpName = "", tmpDensity="";
        bool tmpEnable = false;
        bool _canEdit=false;
        bool _canAdd = false;
        bool _NeedEdit = false;
        loading load;
        ListViewItem[] item;
        public bool canEdit
        {
            get { return _canEdit; }
            set { 
                _canEdit = value;
                btnEdit.Enabled = _canEdit;
                if (!_canEdit)
                    btnEdit.ForeColor = SystemColors.ScrollBar;
                else
                    btnEdit.ForeColor = Color.Black;
            }
        }
        public bool canAdd
        {
            get { return _canAdd; }
            set
            {
                _canAdd = value;
                btnAdd.Enabled = _canAdd;
                if (!_canAdd)
                    btnAdd.ForeColor = SystemColors.ScrollBar;
                else
                    btnAdd.ForeColor = Color.Black;
            }
        }

        string[] OldValue = new string[5];

        Database _Database = null;
        public frmIngredient()
        {
            InitializeComponent();
        }

        private void frmIngredient_Load(object sender, EventArgs e)
        {            
           
            canEdit = false;
            load = new loading();
            txtType.SelectedIndex = 0;
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            btnShow.BackColor = Color.Green;
            btnShow.ForeColor = Color.Lime;
            btnNotShow.BackColor = SystemColors.Control;
            btnNotShow.ForeColor = SystemColors.ScrollBar;
            ShowNotUse = true;
        }
        private void btnNotShow_Click(object sender, EventArgs e)
        {
            btnNotShow.BackColor = Color.Green;
            btnNotShow.ForeColor = Color.Lime;
            btnShow.BackColor = SystemColors.Control;
            btnShow.ForeColor = SystemColors.ScrollBar;
            ShowNotUse = false;
        }
        private void btnExe_Click(object sender, EventArgs e)
        {
            QueyList();            
        }

        private void QueyList()
        {           
            _Database = new Database();
            sql = "select * from dbo_Ingredient where (IngredientCode like @Keyword or IngredientName like @Keyword) ";
            if (!ShowNotUse)
                sql += " and Enable = 1";
            List<OleDbParameter> parameters = new List<OleDbParameter>();
            parameters.Add(new OleDbParameter("Keyword", "%" + txtKeyword.Text + "%") );
            DataTable data = _Database.Query(sql, parameters);            
            if(data.Rows.Count == 0){ 
                data.Rows.Add("No Result");
            }
            listIngredient.DataSource = data;
            listIngredient.Columns[4].Visible = false;
            listIngredient.AutoResizeColumns();
           
                     
        }
       
        private void clearBox()
        {
            txtCode.Text = "";
            txtDensity.Text = "";
            txtType.SelectedIndex = 0;
            txtName.Text = "";
            txtCode.BackColor = Color.White;
            txtDensity.BackColor = Color.White;
            txtType.BackColor = Color.White;
            txtName.BackColor = Color.White;
            cbInUse.Checked = false;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {            
            StringBuilder movecmd = new StringBuilder();
            movecmd.Append("INSERT INTO dbo_IngredientLog (IngredientCode,IngredientName,IngredientType, Density, Enable, UpdateTime,UpdateTimeLog)");
            movecmd.Append("SELECT IngredientCode,IngredientName,IngredientType, Density, Enable,UpdateTime,NOW() FROM dbo_Ingredient WHERE IngredientCode = @IngredientCode");
            List<OleDbParameter> parameters = new List<OleDbParameter>();
            parameters.Add(new OleDbParameter("IngredientCode",txtCode.Text.Trim()));
            _Database = new Database();
            _Database.ExecuteNonQuery(movecmd.ToString(), parameters);
            movecmd.Remove(0, movecmd.Length);
            movecmd.Append("UPDATE dbo_Ingredient SET IngredientName=@Name,Density=@Density, Enable=@Enable, UpdateTime=NOW() WHERE IngredientCode=@Code");
            parameters = new List<OleDbParameter>();
            parameters.Add(new OleDbParameter("Name", txtName.Text.Trim()));
            parameters.Add(new OleDbParameter("Density", txtDensity.Text.Trim()));
            int en = cbInUse.Checked ? 1 : 0;
            parameters.Add(new OleDbParameter("Enable", en));
            parameters.Add(new OleDbParameter("Code", txtCode.Text.Trim()));
            _Database.ExecuteNonQuery(movecmd.ToString(), parameters);
            MessageBox.Show("Edit Complete", "Information");
            clearBox();
            
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (canEdit)
            {
                clearBox();
                txtCode.Enabled = true;
                txtDensity.Enabled = true;
                txtType.Enabled = true;
                txtName.Enabled = true;
                canEdit = false;
            }
            else
            {
                if (txtCode.Text.Trim() != "" && txtName.Text.Trim() != "" && txtDensity.Text.Trim() != "" && txtType.SelectedIndex > -1)
                {
                    _NeedEdit = false;
                    if (MessageBox.Show("Are you sure to add " + txtName.Text + " to database", "Comfirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        List<OleDbParameter> parameters = new List<OleDbParameter>();
                        _Database = new Database();
                        sql = "SELECT IngredientCode FROM dbo_Ingredient where IngredientCode = @IngredientCode";
                        parameters.Add(new OleDbParameter("IngredientCode",txtCode.Text.Trim()));
                        int row = _Database.Query(sql,parameters).Rows.Count;
                        if (row == 0)
                        {
                            sql = "INSERT INTO dbo_Ingredient (IngredientCode,IngredientName,IngredientType, Density, Enable, UpdateTime) VALUES(@IngredientCode,@IngredientName,@IngredientType, @Density, @Enable, NOW())";
                            parameters = new List<OleDbParameter>();
                            parameters.Add(new OleDbParameter("IngredientCode", txtCode.Text.Trim()));
                            parameters.Add(new OleDbParameter("IngredientName", txtName.Text.Trim()));
                            parameters.Add(new OleDbParameter("IngredientType", txtType.Text.Trim()));
                            parameters.Add(new OleDbParameter("Density", txtDensity.Text.Trim()));
                            int en = cbInUse.Checked ? 1 : 0;
                            parameters.Add(new OleDbParameter("Enable", en));
                            _Database.ExecuteNonQuery(sql, parameters);
                        }
                        else
                            MessageBox.Show("Duplicate IngredientCode","Error");


                    }
                    clearBox();
                }
                else
                {
                    MessageBox.Show("Please check the red box and add agian", "Warning");
                    _NeedEdit = true;
                    if (txtCode.Text.Trim() == "")
                        txtCode.BackColor = Color.Red;
                    if(txtDensity.Text.Trim() == "")
                        txtDensity.BackColor = Color.Red;
                    if(txtType.SelectedIndex <0)
                        txtType.BackColor = Color.Red;
                    if(txtName.Text.Trim() == "")
                        txtName.BackColor = Color.Red;
                }
            }

        }
         
        private void txtDensity_KeyPress(object sender, KeyPressEventArgs e)
        {
            if((txtDensity.Text.Trim()+e.KeyChar.ToString()).Split('.').Length > 2)
                e.Handled = true;
            else if (!char.IsNumber(e.KeyChar) && (Keys)e.KeyChar != Keys.Back && e.KeyChar != '.')
                e.Handled = true;           
        }

        private void txtCode_TextChanged(object sender, EventArgs e)
        {
            if (_NeedEdit)
            {
                TextBox tmp = (TextBox)sender;
                if (tmp.Text.Trim() != "")
                    tmp.BackColor = Color.LimeGreen;
                else
                    tmp.BackColor = Color.Red;
            }
        }

        private void listIngredient_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            _NeedEdit = false;
                        txtCode.Enabled = false;
                        txtType.Enabled = false;
                        canEdit = true;
                        canAdd = true;
                        tmpCode = txtCode.Text = listIngredient.Rows[e.RowIndex].Cells[0].Value.ToString();
                        txtName.Text = listIngredient.Rows[e.RowIndex].Cells[1].Value.ToString();
                        tmpName = txtName.Text;
                        txtType.Text = listIngredient.Rows[e.RowIndex].Cells[2].Value.ToString();
                        txtDensity.Text = listIngredient.Rows[e.RowIndex].Cells[3].Value.ToString();
                        tmpDensity = txtDensity.Text;
                        tmpEnable = (listIngredient.Rows[e.RowIndex].Cells[4].Value.ToString() == "1");
                        cbInUse.Checked = tmpEnable?true:false;
        }
                
    }
}